dwcm_get_lacteria_test_subject = {
	if = { # We check if our last test subject is still available
		limit = {
			event_target:dwcm_lacteria_test_subject = { 
				character = yes
				is_alive = yes
				prisoner = no
				is_incapable = no
				OR = {
					has_global_flag = Ala_Body_Mod
					has_dw_bodytrait_content = yes
				}
			} 
		}
	}
	else_if = { # Otherwise, we check to see if we've another subject we can use
		limit = {
			location = {
				any_province_character = {
					is_female = yes
					is_adult = yes
					age < 40
					prisoner = no
					is_incapable = no
					ai = yes #we don't want the player being pulled into these events against their will
					OR = {
						has_global_flag = Ala_Body_Mod
						has_dw_bodytrait_content = yes
					}
				}
			}
		}
		#If there is, we then grab one for the event
		location = {
			random_province_character = {
				limit = {
					is_female = yes
					is_adult = yes
					age < 40
					prisoner = no
					is_incapable = no
					ai = yes
					OR = {
						has_global_flag = Ala_Body_Mod
						has_dw_bodytrait_content = yes
					}
				}
				save_event_target_as = dwcm_lacteria_test_subject
			}
		}
	}
}

dwcm_become_hucow = { # [Modder_friendly] Make the character a hucow and initialize her variables
	if = {
		limit = { NOT = { trait = hucow } }
		add_trait = hucow
		hidden_tooltip = {
			set_variable = { which = dwcm_milk_quantity_CURRENT value = 0 }
			set_variable = { which = dwcm_tracker_milk_produced value = 0 }
			set_variable = { which = dwcm_tracker_time_milked value = 0 }
			dwcm_update_milk_production_MULTIPLIER = yes
			dwcm_update_milk_variables = yes
			add_character_modifier = {
				name = dwcm_milk_capacity_counter
				duration = -1
			}
			set_character_flag = dwcm_hucow_variables_initialized
		}
	}
}

#
# Hucow caracteristics calculations (Milk capacity, production rate...)
#
dwcm_increase_current_milk_quantity = { # [Modder_friendly] Increase milk quantity by one tick (dwcm_milk_production_rate_TOTAL)
	if = {
		limit = { NOT = { check_variable = { which = dwcm_milk_quantity_CURRENT which = dwcm_milk_capacity_TOTAL } } }
		dwcm_update_milk_production_rate_TOTAL = yes
		change_variable = { which = dwcm_milk_quantity_CURRENT which = dwcm_milk_production_rate_TOTAL }
		if = {
			limit = { check_variable = { which = dwcm_milk_quantity_CURRENT which = dwcm_milk_capacity_TOTAL } }
			set_variable = { which = dwcm_milk_quantity_CURRENT which = dwcm_milk_capacity_TOTAL }
		}
	}
	dwcm_update_divided_milk_quantities = yes
}

dwcm_update_milk_variables = { # [Modder_friendly] Update most milk related variables
	dwcm_update_divided_milk_quantities = yes
	dwcm_update_milk_capacity_raw = yes
	dwcm_update_milk_capacity_TOTAL = yes
	dwcm_update_milk_capacity_Engorged = yes
	dwcm_update_milk_production_rate_raw = yes
	dwcm_update_milk_production_rate_TOTAL = yes
}

dwcm_update_divided_milk_quantities = {
	set_variable = { which = dwcm_milk_quantity_half which = dwcm_milk_quantity_CURRENT }
	divide_variable = { which = dwcm_milk_quantity_half value = 2 }
	set_variable = { which = dwcm_milk_quantity_third which = dwcm_milk_quantity_CURRENT }
	divide_variable = { which = dwcm_milk_quantity_third value = 3 }
	set_variable = { which = dwcm_milk_quantity_quarter which = dwcm_milk_quantity_CURRENT }
	divide_variable = { which = dwcm_milk_quantity_quarter value = 4 }
}

dwcm_update_milk_capacity_raw = { # Raw Milk Capacity (quantity of milk a hucow can store in her breasts. Without other modifiers)
	trigger_switch = {
		on_trigger = trait
		dw_gigantic_tits = { set_variable = { which = dwcm_milk_capacity_raw value = 3.5 } }
		dw_enormous_tits = { set_variable = { which = dwcm_milk_capacity_raw value = 3 } }
		dw_huge_tits = { set_variable = { which = dwcm_milk_capacity_raw value = 2.5 } }
		dw_big_tits = { set_variable = { which = dwcm_milk_capacity_raw value = 2 } }
		dw_regular_tits = { set_variable = { which = dwcm_milk_capacity_raw value = 1.5 } }
		dw_small_tits = { set_variable = { which = dwcm_milk_capacity_raw value = 1 } }
		dw_tiny_tits = { set_variable = { which = dwcm_milk_capacity_raw value = 0.5 } }
		ala_gigantic_tits = { set_variable = { which = dwcm_milk_capacity_raw value = 3.5 } }
		ala_enormous_tits = { set_variable = { which = dwcm_milk_capacity_raw value = 3 } }
		ala_huge_tits = { set_variable = { which = dwcm_milk_capacity_raw value = 2.5 } }
		ala_big_tits = { set_variable = { which = dwcm_milk_capacity_raw value = 2 } }
		ala_regular_tits = { set_variable = { which = dwcm_milk_capacity_raw value = 1.5 } }
		ala_small_tits = { set_variable = { which = dwcm_milk_capacity_raw value = 1 } }
		ala_tiny_tits = { set_variable = { which = dwcm_milk_capacity_raw value = 0.5 } }
	}
}

dwcm_update_milk_capacity_TOTAL = { # Actual Milk Capacity (including potential modifiers)
	set_variable = { which = dwcm_milk_capacity_TOTAL which = dwcm_milk_capacity_raw }
	#TODO Add potential milk capacity bonuses
}

dwcm_update_milk_capacity_Engorged = { # Engorged Milk Capacity (including potential modifiers)
	set_variable = { which = dwcm_milk_capacity_Engorged which = dwcm_milk_capacity_TOTAL }
	multiply_variable = { which = dwcm_milk_capacity_Engorged value = 0.75 }
	#TODO Add potential milk capacity bonuses
}

dwcm_update_milk_production_rate_raw = { # Raw Milk Production Rate (Quantity of milk produced each tick (about 6 months)).
	trigger_switch = {
		on_trigger = trait
		dw_gigantic_tits = { set_variable = { which = dwcm_milk_production_rate_raw value = 0.5 } }
		dw_enormous_tits = { set_variable = { which = dwcm_milk_production_rate_raw value = 0.47 } }
		dw_huge_tits = { set_variable = { which = dwcm_milk_production_rate_raw value = 0.41 } }
		dw_big_tits = { set_variable = { which = dwcm_milk_production_rate_raw value = 0.34 } }
		dw_regular_tits = { set_variable = { which = dwcm_milk_production_rate_raw value = 0.26 } }
		dw_small_tits = { set_variable = { which = dwcm_milk_production_rate_raw value = 0.18 } }
		dw_tiny_tits = { set_variable = { which = dwcm_milk_production_rate_raw value = 0.12 } }
		ala_gigantic_tits = { set_variable = { which = dwcm_milk_production_rate_raw value = 0.5 } }
		ala_enormous_tits = { set_variable = { which = dwcm_milk_production_rate_raw value = 0.47 } }
		ala_huge_tits = { set_variable = { which = dwcm_milk_production_rate_raw value = 0.41 } }
		ala_big_tits = { set_variable = { which = dwcm_milk_production_rate_raw value = 0.34 } }
		ala_regular_tits = { set_variable = { which = dwcm_milk_production_rate_raw value = 0.26 } }
		ala_small_tits = { set_variable = { which = dwcm_milk_production_rate_raw value = 0.18 } }
		ala_tiny_tits = { set_variable = { which = dwcm_milk_production_rate_raw value = 0.12 } }
	}
}

dwcm_update_milk_production_MULTIPLIER = { # Milk Production Multiplier (Is used to alter production rate)
	set_variable = { which = dwcm_milk_production_MULTIPLIER value = 1 }
	#TODO Add potential multipliers (idea: pregnancy, fuckable nipples from DWF: tentacles, etc...)
}

dwcm_raise_milk_production_MULTIPLIER_15 = { # Milk Production Multiplier (Is used to alter production rate)
	multiply_variable = { which = dwcm_milk_production_MULTIPLIER value = 1.15 }
	#TODO Add potential multipliers (idea: pregnancy, fuckable nipples from DWF: tentacles, etc...)
}

dwcm_raise_milk_production_MULTIPLIER_10 = { # Milk Production Multiplier (Is used to alter production rate)
	multiply_variable = { which = dwcm_milk_production_MULTIPLIER value = 1.1 }
	#TODO Add potential multipliers (idea: pregnancy, fuckable nipples from DWF: tentacles, etc...)
}

dwcm_raise_milk_production_MULTIPLIER_5 = { # Milk Production Multiplier (Is used to alter production rate)
	multiply_variable = { which = dwcm_milk_production_MULTIPLIER value = 1.05 }
	#TODO Add potential multipliers (idea: pregnancy, fuckable nipples from DWF: tentacles, etc...)
}

dwcm_update_milk_production_rate_TOTAL = { # Actual Production Rate (after multiplier being applied)
	set_variable = { which = dwcm_milk_production_rate_TOTAL which = dwcm_milk_production_rate_raw }
	multiply_variable = { which = dwcm_milk_production_rate_TOTAL which = dwcm_milk_production_MULTIPLIER }
	
	random = {
		chance = 10
		multiply_variable = { which = dwcm_milk_production_rate_TOTAL value = 0.8 }
	}
	random = {
		chance = 20
		multiply_variable = { which = dwcm_milk_production_rate_TOTAL value = 0.85 }
	}
	random = {
		chance = 30
		multiply_variable = { which = dwcm_milk_production_rate_TOTAL value = 0.9 }
	}
	random = {
		chance = 40
		multiply_variable = { which = dwcm_milk_production_rate_TOTAL value = 0.95 }
	}
	random = {
		chance = 40
		multiply_variable = { which = dwcm_milk_production_rate_TOTAL value = 1.05 }
	}
	random = {
		chance = 30
		multiply_variable = { which = dwcm_milk_production_rate_TOTAL value = 1.1 }
	}
	random = {
		chance = 20
		multiply_variable = { which = dwcm_milk_production_rate_TOTAL value = 1.15 }
	}
	random = {
		chance = 10
		multiply_variable = { which = dwcm_milk_production_rate_TOTAL value = 1.2 }
	}
}

dwcm_breast_growth = {
 	#DW body
	if = {
		limit = { trait = dw_enormous_tits }
		remove_trait = dw_enormous_tits
		add_trait = dw_gigantic_tits
	}
	else_if = {
		limit = { trait = dw_huge_tits }
		remove_trait = dw_huge_tits
		add_trait = dw_enormous_tits
	}
	else_if = {
		limit = { trait = dw_big_tits }
		remove_trait = dw_big_tits
		add_trait = dw_huge_tits
	}
	else_if = {
		limit = { trait = dw_regular_tits }
		remove_trait = dw_regular_tits
		add_trait = dw_big_tits
	}
	else_if = {
		limit = { trait = dw_small_tits }
		remove_trait = dw_small_tits
		add_trait = dw_regular_tits
	}
	else_if = {
		limit = { trait = dw_tiny_tits }
		remove_trait = dw_tiny_tits
		add_trait = dw_small_tits
	}
	#ALA body
	else_if = {
		limit = { trait = ala_enormous_tits }
		remove_trait = ala_enormous_tits
		add_trait = ala_gigantic_tits
	}
	else_if = {
		limit = { trait = ala_huge_tits }
		remove_trait = ala_huge_tits
		add_trait = ala_enormous_tits
	}
	else_if = {
		limit = { trait = ala_big_tits }
		remove_trait = ala_big_tits
		add_trait = ala_huge_tits
	}
	else_if = {
		limit = { trait = ala_regular_tits }
		remove_trait = ala_regular_tits
		add_trait = ala_big_tits
	}
	else_if = {
		limit = { trait = ala_small_tits }
		remove_trait = ala_small_tits
		add_trait = ala_regular_tits
	}
	else_if = {
		limit = { trait = ala_tiny_tits }
		remove_trait = ala_tiny_tits
		add_trait = ala_small_tits
	}
}

#
# Milking effects
#
dcwm_milking_after_effect = { # Don't call this scripted effect directly, use the other milk effects bellow, or set up the right variables beforehand
	if = {
		limit = {
			trait = engorged_breasts
		}
		remove_trait = engorged_breasts	
	}
	if = {
		limit = {
			trait = stressed
			learning <= 5
		}
		remove_trait = stressed
	}
	if = {
		limit = {
			trait = depressed
			learning <= 5
		}
		remove_trait = depressed
	}
	if = {
		limit = {
			trait = stressed
			learning > 5
			learning < 10
		}
		random = {
			chance = 10
			additive_modifier = {
				value = 40 # bumps it up to 50% (change to your liking)
				has_character_flag = milked_full
			}
			additive_modifier = {
				value = 25 # bumps it up to 35% (change to your liking)
				has_character_flag = milked_half
			}
			additive_modifier = {
				value = 10 # bumps it up to 20% (change to your liking)
				has_character_flag = milked_third
			}
			remove_trait = stressed
		}
	}
	if = {
		limit = {
			trait = depressed
			learning > 5
			learning < 10
		}
		random = {
			chance = 10
			additive_modifier = {
				value = 40 # bumps it up to 50% (change to your liking)
				has_character_flag = milked_full
			}
			additive_modifier = {
				value = 25 # bumps it up to 35% (change to your liking)
				has_character_flag = milked_half
			}
			additive_modifier = {
				value = 10 # bumps it up to 20% (change to your liking)
				has_character_flag = milked_third
			}
			remove_trait = depressed
		}
	}
	add_character_modifier = {
		modifier = recently_milked
		duration = 30
	}
	hidden_tooltip = {
		change_variable = { which = dwcm_tracker_milk_produced which = local_dwcm_milked_amount }
		change_variable = { which = dwcm_tracker_time_milked value = 1 }
		dwcm_update_divided_milk_quantities = yes
	}
	clr_character_flag = milked_full
	clr_character_flag = milked_half
	clr_character_flag = milked_third
}

dcwm_milk_all_barn_wealth_effect = { # [Modder_friendly] Milk all the milk present in the character's breasts, add gold
	hidden_tooltip = { set_variable = { which = local_dwcm_milked_amount which = dwcm_milk_quantity_CURRENT } }
	wealth = dwcm_milk_quantity_CURRENT
	prestige = -15
	subtract_variable = { which = dwcm_milk_quantity_CURRENT  which = dwcm_milk_quantity_CURRENT }
	set_character_flag = milked_full
	dcwm_milking_after_effect = yes
}

dcwm_milk_all_troops_effect = { # [Modder_friendly] Milk all the milk present in the character's breasts, boost troops morale
	hidden_tooltip = { set_variable = { which = local_dwcm_milked_amount which = dwcm_milk_quantity_CURRENT } }
	subtract_variable = { which = dwcm_milk_quantity_CURRENT  which = dwcm_milk_quantity_CURRENT }
	set_character_flag = milked_full
	dcwm_milking_after_effect = yes
	add_character_modifier = {
		modifier = dcwm_morale_booster
		duration = 30
	}
}

dcwm_milk_all_barn_effect = { # [Modder_friendly] Milk all the milk present in the character's breasts
	hidden_tooltip = { set_variable = { which = local_tmp_milked_amount which = dwcm_milk_quantity_CURRENT } }
	hidden_tooltip = { set_variable = { which = local_dwcm_milked_amount which = dwcm_milk_quantity_CURRENT } }
	subtract_variable = { which = dwcm_milk_quantity_CURRENT  which = dwcm_milk_quantity_CURRENT }
	prestige = -5
	set_character_flag  = milked_full
	dcwm_milking_after_effect = yes
}

dcwm_milk_all_effect = { # [Modder_friendly] Milk all the milk present in the character's breasts
	hidden_tooltip = { set_variable = { which = local_tmp_milked_amount which = dwcm_milk_quantity_CURRENT } }
	hidden_tooltip = { set_variable = { which = local_dwcm_milked_amount which = dwcm_milk_quantity_CURRENT } }
	subtract_variable = { which = dwcm_milk_quantity_CURRENT  which = dwcm_milk_quantity_CURRENT }
	set_character_flag  = milked_full
	dcwm_milking_after_effect = yes
}

dcwm_milk_half_effect = { # [Modder_friendly] Milk half the milk present in the character's breasts
	hidden_tooltip = { set_variable = { which = local_dwcm_milked_amount which = dwcm_milk_quantity_half } }
	subtract_variable = { which = dwcm_milk_quantity_CURRENT  which = dwcm_milk_quantity_half }
	set_character_flag  = milked_half
	dcwm_milking_after_effect = yes
}

dcwm_milk_third_effect = { # [Modder_friendly] Milk a third of the milk present in the character's breasts
	hidden_tooltip = { set_variable = { which = local_dwcm_milked_amount which = dwcm_milk_quantity_third } }
	subtract_variable = { which = dwcm_milk_quantity_CURRENT  which = dwcm_milk_quantity_third }
	set_character_flag  = milked_third
	dcwm_milking_after_effect = yes
}

dcwm_milk_quarter_effect = { # [Modder_friendly] Milk a quarter of the milk present in the character's breasts
	hidden_tooltip = { set_variable = { which = local_dwcm_milked_amount which = dwcm_milk_quantity_quarter } }
	subtract_variable = { which = dwcm_milk_quantity_CURRENT  which = dwcm_milk_quantity_quarter }
	dcwm_milking_after_effect = yes
}